
import { login } from '@/api/system/total-api'
import { setToken, removeToken } from '@/utils/auth'
import router from '@/router'
import store from '@/store'
import { Message } from 'element-ui'


const state = {
  user: null
}
const mutations = {
  RESET_USER: (state) => {
    state.user = null
  },
  SET_USER: (state, user) => {
    state.user = user
  },
}
const actions = {
  // user login
  login({ commit }, params) {
    //保存token
    setToken('test-token');
    //跳转到home 
    router.push('/')
    store.dispatch("permission/generateRoutes")
    // return login(params).then((res) => {
    //   if (res.data.code === 200) {
    //     //保存token
    //     setToken(res.data.data.token);
    //     //跳转到home 
    //     router.push('/')
    //     commit('SET_USER', res.data.data)
    //     store.dispatch("permission/generateRoutes")
    //   } else {
    //     Message.error(res.data.msg);
    //   }
    //   return res
    // })
  },

  // user logout
  logout({ commit }) {
    return new Promise((resolve, reject) => {
      removeToken()
      router.replace({ name: "login" });
      commit('RESET_USER')
      resolve()
    })
  },

}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

